Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,294 members, 7,807,999 topics. Date: Thursday, 25 April 2024 at 02:00 AM

A Little Javascript Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / A Little Javascript Problem (3270 Views)

Cohort 1, Learn How To Program With HTML, CSS And Javascript For A Little Token / After 5 Months Of Learning OOP In Visual C#-here Is A Little Project I Made. / How To Learn Programming A Little Fast (2) (3) (4)

(1) (2) (Reply) (Go Down)

A Little Javascript Problem by Kodejuice: 7:10pm On Dec 24, 2016
Found this online, and it looks so impossible but its very much possible......so if you think youre a JavaScript Baddass/Ninja or wareva, here's the chance to prove urself right.


In fact, this isn't about JavaScript, but that's the context I've discussed it in. I encourage you to think about it in more programming languages. (are there languages in which this can't be done?)

The problem: define functions range, map, reverse and foreach, obeying the restrictions below, such that the following program works properly. It prints the squares of numbers from 1 to 10, in reverse order.

var numbers = range(1, 10);
numbers = map(numbers, function (n) { return n * n });
numbers = reverse(numbers);
foreach(numbers, console.log);

/* output:

100
81
64
49
36
25
16
9
4
1
*/

Restrictions

You must not use arrays. The square bracket characters, [ and ], are forbidden, as well as new Array.

You must not use objects. The curly braces, { and }, and the dot character (.) are forbidden. You may use curly braces for code blocks, but not for creating JavaScript objects.

Should go without saying, these functions must be generic and do what their name implies. They must not be hard-coded for the particular 1..10 example.

Feel free to define utilities; you don't have to restrict your program to these 4 functions. It does not matter how fast, small or elegant it is — if you can do it within the limitations above, I think you're an above-average programmer.
Re: A Little Javascript Problem by paranorman(m): 9:17pm On Dec 24, 2016
I will post my solution tomorrow. I like challenges.
Re: A Little Javascript Problem by Craigston: 1:02am On Dec 25, 2016
Let me watch the big techies solve this.
Re: A Little Javascript Problem by ANTONINEUTRON(m): 9:41am On Dec 25, 2016
Are U Saying We Must Follow The Method Above(i.e Use The Functions Exactly) Pls Explain



Also U Mean People Can't Use Document.Write?
Re: A Little Javascript Problem by godofbrowser(m): 12:12pm On Dec 25, 2016
Kodejuice:
Found this online, and it looks so impossible but its very much possible......so if you think youre a JavaScript Baddass/Ninja or wareva, here's the chance to prove urself right.


In fact, this isn't about JavaScript, but that's the context I've discussed it in. I encourage you to think about it in more programming languages. (are there languages in which this can't be done?)

The problem: define functions range, map, reverse and foreach, obeying the restrictions below, such that the following program works properly. It prints the squares of numbers from 1 to 10, in reverse order.

var numbers = range(1, 10);
numbers = map(numbers, function (n) { return n * n });
numbers = reverse(numbers);
foreach(numbers, console.log);

/* output:

100
81
64
49
36
25
16
9
4
1
*/

Restrictions

You must not use arrays. The square bracket characters, [ and ], are forbidden, as well as new Array.

You must not use objects. The curly braces, { and }, and the dot character (.) are forbidden. You may use curly braces for code blocks, but not for creating JavaScript objects.

Should go without saying, these functions must be generic and do what their name implies. They must not be hard-coded for the particular 1..10 example.

Feel free to define utilities; you don't have to restrict your program to these 4 functions. It does not matter how fast, small or elegant it is — if you can do it within the limitations above, I think you're an above-average programmer.


var range = Array.from(Array(11).keys());

Array.shift(range);
var numbers = Array.reverse(Array.map(range, function(value){
return value * value;
}));

Array.forEach(numbers, logNumbers);

function logNumbers(v){
console.log(v);
}


Does this fit into your desired solution?

Note: i have tried to avoid using a "new Array" or the square brackets "[]" array declaration. I have also tried not to use the "numbers" var as an object which i know it is (everything in js is an object).
Re: A Little Javascript Problem by Nobody: 2:30pm On Dec 25, 2016
Why are you guys like this? This is xmas, you ought to be killing turkeys, rams, goats, cows etc or at least eating one somewhere, but why?
You guys just wanna code year in, year out, no way, i am out of here, runs away at top speed!!!!!!!
Re: A Little Javascript Problem by Craigston: 3:07pm On Dec 25, 2016
dhtml18:
Why are you guys like this? This is xmas, you ought to be killing turkeys, rams, goats, cows etc or at least eating one somewhere, but why?
You guys just wanna code year in, year out, no way, i am out of here, runs away at top speed!!!!!!!
Don't mind them. Coding like bots. Oya choose your favorite: turkey, beef, venison, snake, crocodile, antelope, grass cutter, pork, or rat. You have at most two choices smiley
Re: A Little Javascript Problem by Kodejuice: 3:33pm On Dec 25, 2016
godofbrowser:



var range = Array.from(Array(11).keys());

Array.shift(range);
var numbers = Array.reverse(Array.map(range, function(value){
return value * value;
}));

Array.forEach(numbers, logNumbers);

function logNumbers(v){
console.log(v);
}


Does this fit into your desired solution?

Note: i have tried to avoid using a "new Array" or the square brackets "[]" array declaration. I have also tried not to use the "numbers" var as an object which i know it is (everything in js is an object).

LoL, ur solution violates the rules,
1. You used the dot character
2. define the functions and not Array.wareva
Re: A Little Javascript Problem by stack1(m): 12:53am On Dec 26, 2016
  function range(start, end) {

var result = start.toString();
while (start < end)
result += "," + ((++start).toString());

return result.split("," );

}
function map(arr, func) {
var temp = arr.slice(0, arr.length);//make a local copy

//clear out the original array
while (arr.length)
arr.shift();

while (temp.length)
arr.push(func(temp.shift())); //send each element in temp to the callback and add the result back to arr
return arr;
}
function reverse(arr) {

return arr.reverse();
}

function foreach(arr, func) {

while(arr.length )func.bind(console)(arr.shift());
}


Ok, you are probably going to say i used the dot operator... working on a version 2
Re: A Little Javascript Problem by stack1(m): 2:05am On Dec 26, 2016
...So this is version 2, i removed all but one dot operator, i couldn't find an alternative for the
last dot operator in the foreach function so..
(and i know, the with operator feels like a cheat)


function range(start, end) {

var result = String(start);
while (start < end)
result += "," + String(++start);

with (result) {
return split("," );
}


}
function map(arr, func) {
var l, m, n, o;

with (arr) {
l = length;
var temp = slice(0, l);//make a local copy

//clear out the original array
while (length)
shift();
}

m = 0;
while ((m++) !== l) {
with (temp) {
n = shift();
}
o = func(n);
with (arr) {
push(o);
}
}
return arr;
}
function reverse(arr) {
with(arr) return reverse();
}
function foreach(arr, func) {
var current, i=0,l;

with(arr){
l = length;
}
while ( (i++) !== l){

with(arr){
current = shift();
}
func.bind(console)(current);
}

}
Re: A Little Javascript Problem by paranorman(m): 1:15pm On Dec 26, 2016
i did PHP, using a recursive function, Mr. kodejuice
function jarvis(&$i, &$j, &$count) {
//$j = 10, by default, equivalent to the max number
//of the range;

$rand = rand(1, 10);
if ($j > 0) {
//is the first instance of the generated random number = 10 ? (print the number) : (keep looping through);
if (($rand == max(range(1, 10))) && ($count == 0)) {
print pow($rand, 2) . '<br />';
$j--;
$i = $rand;
$count++;
jarvis($i, $j, $count);
} else if ($rand == $j) {
//check if it's the next instance whose product
//should be printed; is instance consecutive?

print pow($rand, 2) . '<br />';
$j--;
$i = $rand;
jarvis($i, $j, $count);
} else {
//keep loopin through if the nth instance is not consecutive
//wrt to the previous

jarvis($i, $j, $count);
}
}
}

function ironMan() {
$i = 0;
$j = 10;
$count = 0;
//the $i parameter is not necessary, just included in function construct in case
//you wanna track the random number

jarvis($i, $j, $count);
}
ironMan();
Re: A Little Javascript Problem by Kodejuice: 4:17pm On Dec 26, 2016
paranorman:
i did PHP, using a recursive function, Mr. kodejuice
function jarvis(&$i, &$j, &$count) {
//$j = 10, by default, equivalent to the max number
//of the range;

$rand = rand(1, 10);
if ($j > 0) {
//is the first instance of the generated random number = 10 ? (print the number) : (keep looping through);
if (($rand == max(range(1, 10))) && ($count == 0)) {
print pow($rand, 2) . '<br />';
$j--;
$i = $rand;
$count++;
jarvis($i, $j, $count);
} else if ($rand == $j) {
//check if it's the next instance whose product
//should be printed; is instance consecutive?

print pow($rand, 2) . '<br />';
$j--;
$i = $rand;
jarvis($i, $j, $count);
} else {
//keep loopin through if the nth instance is not consecutive
//wrt to the previous

jarvis($i, $j, $count);
}
}
}

function ironMan() {
$i = 0;
$j = 10;
$count = 0;
//the $i parameter is not necessary, just included in function construct in case
//you wanna track the random number

jarvis($i, $j, $count);
}
ironMan();

(WTF)

1 Like 1 Share

Re: A Little Javascript Problem by Kodejuice: 4:25pm On Dec 26, 2016
stack1:
...So this is version 2, i removed all but one dot operator, i couldn't find an alternative for the
last dot operator in the foreach function so..
(and i know, the with operator feels like a cheat)


function range(start, end) {

var result = String(start);
while (start < end)
result += "," + String(++start);

with (result) {
return split("," );
}


}
function map(arr, func) {
var l, m, n, o;

with (arr) {
l = length;
var temp = slice(0, l);//make a local copy

//clear out the original array
while (length)
shift();
}

m = 0;
while ((m++) !== l) {
with (temp) {
n = shift();
}
o = func(n);
with (arr) {
push(o);
}
}
return arr;
}
function reverse(arr) {
with(arr) return reverse();
}
function foreach(arr, func) {
var current, i=0,l;

with(arr){
l = length;
}
while ( (i++) !== l){

with(arr){
current = shift();
}
func.bind(console)(current);
}

}


LoL, u try sha, anyway heres my solution


// helper functions
function cons(x, y) {
return function(f) { return f(x, y) };
}

function car(pair) {
return pair(function(x, y){ return x });
}

function cdr(pair) {
return pair(function(x, y){ return y });
}

function nil(f) {
return f(nil, nil);
}

// ....
function range(start, stop) {
return start <= stop ? cons(start, range(start + 1, stop)) : nil;
}

function map(list, func) {
return list === nil ? nil : cons(func(car(list)), map(cdr(list), func));
}

function foreach(list, func) {
if (list !== nil) {
func(car(list));
foreach(cdr(list), func);
}
}

function reverse(list) {
return (function rev(list, ret){
return list === nil ? ret : rev(cdr(list), cons(car(list), ret));
})(list, nil);
}

1 Like

Re: A Little Javascript Problem by stack1(m): 4:43pm On Dec 26, 2016
Kodejuice:


LoL, u try sha, anyway heres my solution


// helper functions
function cons(x, y) {
return function(f) { return f(x, y) };
}

function car(pair) {
return pair(function(x, y){ return x });
}



function cdr(pair) {
return pair(function(x, y){ return y });
}

function nil(f) {
return f(nil, nil);
}

// ....
function range(start, stop) {
return start <= stop ? cons(start, range(start + 1, stop)) : nil;
}

function map(list, func) {
return list === nil ? nil : cons(func(car(list)), map(cdr(list), func));
}

function foreach(list, func) {
if (list !== nil) {
func(car(list));
foreach(cdr(list), func);
}
}

function reverse(list) {
return (function rev(list, ret){
return list === nil ? ret : rev(cdr(list), cons(car(list), ret));
})(list, nil);
}


Nice tho, lots of closures, guess i was too lazy to write any extra helpers-functions , but cool wink
Re: A Little Javascript Problem by stack1(m): 4:46pm On Dec 26, 2016
any more??
Re: A Little Javascript Problem by paranorman(m): 6:15pm On Dec 26, 2016
Kodejuice:


(WTF)
What the problem? grin
I sha solved the problem my own way with php.
Re: A Little Javascript Problem by romme2u: 12:45am On Dec 27, 2016
op so you use closures and anonymous function finish the life of those utilities.

Maybe you are trying to show us that functions are FIRST CLASS OBJECTS in JS.

###Just pitying the nuts that will maintain and extend those gibberish.###

1 Like

Re: A Little Javascript Problem by Craigston: 8:12am On Dec 27, 2016
paranorman:
i did PHP, using a recursive function, Mr. kodejuice
function jarvis(&$i, &$j, &$count) {
//$j = 10, by default, equivalent to the max number
//of the range;

$rand = rand(1, 10);
if ($j > 0) {
//is the first instance of the generated random number = 10 ? (print the number) : (keep looping through);
if (($rand == max(range(1, 10))) && ($count == 0)) {
print pow($rand, 2) . '<br />';
$j--;
$i = $rand;
$count++;
jarvis($i, $j, $count);
} else if ($rand == $j) {
//check if it's the next instance whose product
//should be printed; is instance consecutive?

print pow($rand, 2) . '<br />';
$j--;
$i = $rand;
jarvis($i, $j, $count);
} else {
//keep loopin through if the nth instance is not consecutive
//wrt to the previous

jarvis($i, $j, $count);
}
}
}

function ironMan() {
$i = 0;
$j = 10;
$count = 0;
//the $i parameter is not necessary, just included in function construct in case
//you wanna track the random number

jarvis($i, $j, $count);
}
ironMan();

Kodejuice:


LoL, u try sha, anyway heres my solution


// helper functions
function cons(x, y) {
return function(f) { return f(x, y) };
}

function car(pair) {
return pair(function(x, y){ return x });
}

function cdr(pair) {
return pair(function(x, y){ return y });
}

function nil(f) {
return f(nil, nil);
}

// ....
function range(start, stop) {
return start <= stop ? cons(start, range(start + 1, stop)) : nil;
}

function map(list, func) {
return list === nil ? nil : cons(func(car(list)), map(cdr(list), func));
}

function foreach(list, func) {
if (list !== nil) {
func(car(list));
foreach(cdr(list), func);
}
}

function reverse(list) {
return (function rev(list, ret){
return list === nil ? ret : rev(cdr(list), cons(car(list), ret));
})(list, nil);
}

Someday these guys are gonna hatch monsters...
Re: A Little Javascript Problem by Nobody: 8:58am On Dec 27, 2016
Craigston:



Someday these guys are gonna hatch monsters...
Are you sure these people are human beings like this? Me, right now, all I can see is turkey, chicken and monitor lizard anywhere i look, no time for coding again o!
Re: A Little Javascript Problem by stack1(m): 9:23am On Dec 27, 2016
dhtml18:

Are you sure these people are human beings like this? Me, right now, all I can see is turkey, chicken and monitor lizard anywhere i look, no time for coding again o!

Monitor Lizard, hahaha cheesy grin
Re: A Little Javascript Problem by Nobody: 10:14am On Dec 27, 2016
stack1:


Monitor Lizard, hahaha cheesy grin
At least Lala dey chop am for FP everything, the reason most threads on this board no dey reach FP na because them no get monitor lizard. If someone here comes up with a code that actually catches a monitor lizard, FP straight. . . .

2 Likes

Re: A Little Javascript Problem by paranorman(m): 12:24pm On Dec 27, 2016
Craigston:



Someday these guys are gonna hatch monsters...
Hahahahaha, I pray so Bros. I am still a rookie sha.

the op was going on about using range, functions bla, bla, bla...

I just solved his problem using a single function, the second function wasn't even necessary.
Re: A Little Javascript Problem by Nobody: 1:14pm On Dec 27, 2016
i dont even know what range means here abi na range wey them teach us for statistics? i don fly fence go play playstation that day
Re: A Little Javascript Problem by Craigston: 1:43pm On Dec 27, 2016
dhtml18:
i dont even know what range means here abi na range wey them teach us for statistics? i don fly fence go play playstation that day
U neva see anything. NA Physics o. Projectiles. One of them don write Jarvis sef (Paranorman). I just tire. Abeg who sabi cook snake wella? My neighbor wan dash me python. E fat ehn! I just want the snake roasting algorithm.
Re: A Little Javascript Problem by Craigston: 1:50pm On Dec 27, 2016
paranorman:

Hahahahaha, I pray so Bros. I am still a rookie sha.

the op was going on about using range, functions bla, bla, bla...

I just solved his problem using a single function, the second function wasn't even necessary.
Your approach might take more time to run. Figure out the cause.
Hint: recursive guessing.
Re: A Little Javascript Problem by Nobody: 2:00pm On Dec 27, 2016
Craigston:

U neva see anything. NA Physics o. Projectiles. One of them don write Jarvis sef (Paranorman). I just tire. Abeg who sabi cook snake wella? My neighbor wan dash me python. E fat ehn! I just want the snake roasting algorithm.
We need picture of the snake, we shall invite lala to cook it for us, how these BOTS are managing to write all these codes in this festive period beats me.
Re: A Little Javascript Problem by paranorman(m): 2:03pm On Dec 27, 2016
Craigston:

Your approach might take more time to run. Figure out the cause.
Hint: recursive guessing.

Sure bro. I've only scratched javascript's surface, ain't pro. Still learning.

My approach will work fine for a small range, but if the range spans accross thousands of data one will grow white beards, especially for a not-so-patient person like me. Besides, it just a challenge anyway.
Re: A Little Javascript Problem by Craigston: 4:02pm On Dec 27, 2016
dhtml18:

We need picture of the snake, we shall invite lala to cook it for us, how these BOTS are managing to write all these codes in this festive period beats me.
Turned out I got egusi soup with plenty stock fish and chicken instead. Bad news for lala, but I'm enjoying myself anyway.
That guy up there (^^^^) surprises me. He just created plenty functions and the whole thing is looking brackety. Shuuuu!!!! Omo botty.
Re: A Little Javascript Problem by Kodejuice: 7:09pm On Dec 27, 2016
paranorman:

Hahahahaha, I pray so Bros. I am still a rookie sha.

the op was going on about using range, functions bla, bla, bla...

I just solved his problem using a single function, the second function wasn't even necessary.

Your solution is OOP (Out Of Point)
undecided

1 Like

Re: A Little Javascript Problem by Kodejuice: 7:16pm On Dec 27, 2016
Re: A Little Javascript Problem by Craigston: 12:20pm On Dec 28, 2016
dhtml18:

At least Lala dey chop am for FP everything, the reason most threads on this board no dey reach FP na because them no get monitor lizard. If someone here comes up with a code that actually catches a monitor lizard, FP straight. . . .
NA true o. Checkout this topic:
https://www.nairaland.com/3540804/see-monitor-lizard-caught-farm
The guy don get the formula.

(1) (2) (Reply)

How To Make Money 49ja (bet9ja) Using Bet Bot / Creative Skills To Improve Your Essay Writing / What Kind Of App Does Nigeria Lack At The Moment?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 59
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.