Nth powers codewars problem

There is one particular test case that isn’t working that was pictured here :



The rest of the test cases are working fine except for that particular one.

Here is the link to my code

you know that you don’t really need the loop? it gives you the index to use as function parameter

you are really complicating things

why your approach doesn’t work? well, indexOf gives the first instance of where in the array that value appear, so if you have n==8 but at index 8 you have a number that was already present in the array, that doesn’t work

so, you are given an array, and the index of the array at which you need to pick the number
how do you access the value of the array at index n?

What approach do you think is better? Why did it work for all of my test cases but not this particular one?

because the n==array.indexOf(array[i]) is never true
this evaluate to 9==0 (indexOf gives the first index at which it finds the item, which is at index 0 as it is an array with only number 1) which is false

how do you access the element at index n of array?

you used the index position function for an array . array[(array.indexOf(array[i]))]

@noblegas87 , I did not understand why do you expect

array.indexOf(1)==9) 

I suggest, first decide what do you exactly want, you will got the point why it’s not working. But before that, close the else if statement properly with else{.....},
if you don’t close it- the result will be making you upset

I used a less complicated function :
function index(array, n){
//your code here

var final;
return array.forEach(function(item, index) {
if(n==item){
final=Math.pow(index,item);
}
})

return final;
}

And now I get this error:

function index(array, n){
//your code here

var final;
let love= array.forEach(function(item, index) {

if(n==index){
final=Math.pow(item,index);
console.log(item,final)
}else if(n>array.length){
console.log(-1);
}
})
return love;

}

console.log(index([1,2,3,4],2));
console.log(index([1,2],3))
console.log(index([29,82,45,10], 3))
console.log(index([75,68,35,61,9,36,89,0,30], 10))

https://codepen.io/noblegas/pen/WNQVELm?editors=1010

I almost found a solution , because I can now print out the output that correesponds to the given input, bu t I don’t know how to return a forEach statement.

@noblegas87, think simple first

function index(array, n) {
    var result ;
    if (array.length > n) {
        result = Math.pow(array[n], n)
    } else {
        result = -1;
    }
    return result;
}

Still I don’t know what do you want but hope this simple code will pass all your tests :grinning:

you can’t, or better, forEach returns undefined

and you really don’t need to iterate over the array, as you know the index at which retrieve the value

I checked his codepen and I am afraid he thinks he solved that by using foreach instead of for loop when he does not suppose to use any sort of loop for this task.

const nth_power = (array,n) => {
    return (n >= array.length) ? -1 : Math.pow(array[n], n);
}

Let’s make it one-liner :smiley:

let result = nth_power([1,1,1,1,1,1,1,1,1,2],9);
console.log(result);
result = nth_power([1,1,1,1,1,1,1,1,1,1],9);
console.log(result)
1 Like

@Dorfieeee, Yes, as you suggested I also love and use this syntax but I didn’t dare to suggest it for @noblegas87 seeing his coding style.

Thank you for making it here for if he could understand…